package recharge

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/consts"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeChannelTenantLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户充值页面--充值通道--收款信息
func NewRechargeChannelTenantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeChannelTenantLogic {
	return &RechargeChannelTenantLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RechargeChannelTenantLogic) RechargeChannelTenant(req *types.RechargeChannelTenantListRequest) (resp *types.RechargeChannelTenantListResponse, err error) {
	resp = &types.RechargeChannelTenantListResponse{}

	switch req.RechargeTypeId {
	case consts.RechargeTypeIdLocalUsdt:
		resp.RechargeLocalUsdt = l.rechargeUsdt()
	}

	return
}

func (l *RechargeChannelTenantLogic) rechargeUsdt() []types.RechargeLocalUsdt {
	var res []types.RechargeLocalUsdt
	dbRes, err := l.svcCtx.AssetsRpc.TenantChannel.UsdtList(l.ctx, &rpc.UsdtListRequest{
		UsdtType: "",
		Page: &rpc.Page{
			Page:     1,
			PageSize: 100,
		},
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return res
	}

	for i := 0; i < len(dbRes.Records); i++ {
		var item = types.RechargeLocalUsdt{
			Id:       dbRes.Records[i].Id,
			Name:     dbRes.Records[i].Name,
			QrCode:   dbRes.Records[i].QrCode,
			UsdtType: dbRes.Records[i].UsdtType,
			Address:  dbRes.Records[i].Address,
			Sort:     int(dbRes.Records[i].Sort),
		}
		res = append(res, item)
	}

	return res
}
